<#include "/@inc/funcMacros.ftl">
<#include "/@inc/copyright.txt">
<#assign className = TriaGram + "E" + Prefix>
<#assign baseName = TriaGram + Prefix >
<#assign intfName = TriaGram + "I" + Prefix>
<@pp.renameOutputFile name=className+"Factory.cpp">
</@pp.renameOutputFile>
//
// ${className}Factory.cpp
// Provide implementation to interface
//      ${intfName}Factory
//===============================================================================
//  ${Month} ${Year}  Creation: Code generated by the CAA wizard  ${Author}
//===============================================================================
#include "${className}Factory.h"

//----${TriaGram}${AppName}Interfaces framework
#include "${intfName}${LateType}.h"
#include "${TriaGram}UtlMacros.h"

//----ObjectSpecsModeler framework (Feature Modeler)
#include "CATICatalog.h"
#include "CATCatalogFactoryServices.h"
#include "CATISpecObject.h"
#include "CATLISTV_CATISpecObject.h"

#include "CATIGSMProceduralView.h"

#include "TIE_${intfName}Factory.h"
TIE_${intfName}Factory( ${className}Factory );

CATImplementClass( ${className}Factory,
                  CodeExtension,
                  CATBaseUnknown,
                  CATFeatCont );

//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
${className}Factory::${className}Factory()
{
}

//-----------------------------------------------------------------------------
// Destructor
//-----------------------------------------------------------------------------
${className}Factory::~${className}Factory()
{
}

//-----------------------------------------------------------------------------
// Implements ${intfName}Factory::Create${LateType}
//-----------------------------------------------------------------------------
HRESULT __stdcall ${className}Factory::Create${LateType} ( CATISpecObject_var &osp${LateType},
														   CATBoolean ibAddToGeomSet)
{
   HRESULT hr = E_FAIL;

   //----Retrieve "${LateType}" Startup
   CATUnicodeString strStartupType = "${TriaGram}${Prefix}${LateType}";

	CATISpecObject* pISpecObjectOnStartup = NULL;
	hr = GetStartup(strStartupType, pISpecObjectOnStartup);
	if ( FAILED(hr) || NULL == pISpecObjectOnStartup)
	{
		return E_FAIL;
	}

	osp${LateType} = pISpecObjectOnStartup ;
    ${TriaGram}UtlReleasePtr(pISpecObjectOnStartup) ;
	if ( NULL_var == osp${LateType})
	{
		hr = E_FAIL;
	}

    // Initialize all CATICKeParms
    ${intfName}${LateType}_var sp${LateType} = osp${LateType} ;
    <#list Attributes as attrib>
    <#if attrib.type?starts_with("tk_param") >
    sp${LateType}->Set${attrib.name}(${attrib.data[0]}) ;
    </#if>
    </#list>

	if(ibAddToGeomSet)
	{
		CATIGSMProceduralView_var spProceduralView = osp${LateType} ;
		if (NULL_var != spProceduralView)
			hr = spProceduralView->InsertInProceduralView() ;
	}
	
	return hr ;
}

HRESULT ${className}Factory::GetStartup( const CATUnicodeString  & ustrType,
									      CATISpecObject  *& opiStartup)
{
	HRESULT rc = E_FAIL ;

	opiStartup = NULL ;
    CATIContainer_var spiContainer = this ;
    if (NULL_var == spiContainer)
	{
       return E_FAIL;
     }

    // Open catalog
    CATICatalog *pi${LateType}Catalog = NULL;
    const CATUnicodeString catalogName("${TriaGram}${Catalog.name}.CATfct");
    const CATUnicodeString clientID("${Catalog.ClientID}");
    rc = ::AccessCatalog(&catalogName,            // Catalog Name
						 &clientID,               // Catalog customer ID
                         spiContainer,             // Target container for instanciation
                         &pi${LateType}Catalog); // Resulting catalog
    if( FAILED(rc)  || NULL == pi${LateType}Catalog )
	{
		//TBD : Make Error. Catalog not in CNext\graphics
        return rc;
	}

    //-------------------------------------------------------------------------------
    // Retrieving startup
    //-------------------------------------------------------------------------------
    CATBaseUnknown *pi${LateType}Startup = NULL;
    rc=pi${LateType}Catalog->RetrieveSU( &pi${LateType}Startup , &ustrType , "CATISpecObject" );
    ${TriaGram}UtlReleasePtr(pi${LateType}Catalog);
    if (FAILED(rc) || (NULL == pi${LateType}Startup))
	{
		return E_FAIL;
	}

    CATISpecObject_var spSpecOn${LateType} = pi${LateType}Startup;
    ${TriaGram}UtlReleasePtr(pi${LateType}Startup) ;
    if( NULL_var ==  spSpecOn${LateType})
	{
        return E_FAIL;
    }

    //-------------------------------------------------------------------------------
    // Instanciate ${LateType} instance
    //-------------------------------------------------------------------------------
    opiStartup = spSpecOn${LateType}->Instanciate( NULL_string , spiContainer );

	return rc ;
}

